package com.ganwei.springcloud.controller;

import com.ganwei.springcloud.entities.CommonResult;
import com.ganwei.springcloud.entities.Payment;
import com.ganwei.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;


    @PostMapping(value = "/payment/create")
    public CommonResult insertPayment(@RequestBody Payment payment) {
        int result = paymentService.insertPayment(payment);
        log.info("***********插入数据");
        if (result > 0) {
            return new CommonResult(200, "插入成功+serverPort端口号：" + serverPort, result);
        } else {
            return new CommonResult(500, "插入失败", result);
        }
    }

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult<Payment> getpaymentById(@PathVariable("id") Long id) {
        Payment payment = paymentService.getpaymentById(id);
        log.info("***********查询数据" + payment);
        if (payment != null) {
            return new CommonResult(200, "查询成功+serverPort端口号：" + serverPort, payment);
        } else {
            return new CommonResult(500, "查询失败，没有对应id" + id, payment);
        }
    }

    @GetMapping(value = "/payment/lb")
    public String getPaymentLB(){
        return serverPort;
    }

    @GetMapping(value = "/payment/feign/timeout")
    public String paymentFeignTimeout(){
        try{
            TimeUnit.SECONDS.sleep(3);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        return serverPort;
    }
}
